package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.UpgradeDetail;
import com.tbit.uqbike.object.pojo.dto.UpgradeDTO;
import com.tbit.uqbike.object.pojo.vo.UpgradeImportVO;
import com.tbit.uqbike.object.pojo.vo.UpgradeVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.UpgradeService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 升级车辆Controller
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2024/3/15
 */
@RestController
@RequestMapping("/upgrade")
public class UpgradeController {

    @Resource
    private UpgradeService upgradeService;


    /**
     * 分页查询车辆列表
     *
     * @param upgradeDTO upgradeDTO
     * @return Result<PageInfo < UpgradeVO>>
     */
    @GetMapping("/queryPage")
    public Result<PageResult<UpgradeVO>> queryPage(@Valid UpgradeDTO upgradeDTO) {
        return upgradeService.queryPage(upgradeDTO);
    }

    /**
     * 导入车辆
     *
     * @param file    excel
     * @param request request
     * @return Result<Object>
     */
    @PostMapping("/importFile")
    public Result<Object> importFile(MultipartFile file, Integer accountId, HttpServletRequest request) {
        if (Objects.isNull(accountId)) {
            return Result.error("请选择区域");
        }
        return upgradeService.importFile(file, accountId, BaseController.getLogin(request));
    }


    /**
     * 删除车辆
     *
     * @param upgradeId 升级车辆id
     * @return Result
     */
    @DeleteMapping("/delete")
    public Result<Object> delete(Integer upgradeId) {
        upgradeService.deleteId(upgradeId);
        return Result.success();
    }

    /**
     * 导出升级车辆信息
     *
     * @param upgradeDTO upgradeDTO
     * @return excel
     */
    @GetMapping("/exportFile")
    public Result<Object> exportFile(UpgradeDTO upgradeDTO) {
        if (Objects.isNull(upgradeDTO.getAccountId())) {
            return Result.error("请选择区域");
        }
        return upgradeService.exportFile(upgradeDTO);
    }

    /**
     * 导入模板
     *
     * @return Result<Object>
     */
    @GetMapping("/exportTemplate")
    public Result<Object> exportTemplate() {
        List<UpgradeImportVO> list = new ArrayList<>();
        UpgradeImportVO upgradeVO = new UpgradeImportVO();
        upgradeVO.setUserCode("123456789");
        list.add(upgradeVO);
        ExcelUtil<UpgradeImportVO> excelUtil = new ExcelUtil<>(UpgradeImportVO.class);
        JsonResult result = excelUtil.exportExcel(list, "升级车辆列表");
        return Result.success(result.getData());
    }

    /**
     * 升级详情
     *
     * @param upgradeId 升级车辆id
     * @return Result
     */
    @GetMapping("/detail")
    public Result<PageResult<UpgradeDetail>> detail(Integer upgradeId, Integer accountId, Integer pageNumber, Integer size) {
        if (Objects.isNull(upgradeId)) {
            return Result.error("请选择车辆");
        }
        if (Objects.isNull(accountId)) {
            return Result.error("区域id为空");
        }
        if (Objects.isNull(pageNumber) || Objects.isNull(size)){
            return Result.error("分页参数不能为空");
        }
        return upgradeService.detail(upgradeId, accountId, pageNumber, size);
    }


}
